﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xamarin.Forms;

namespace SharedMobilePlatform
{
	public class MenuPage : ContentPage
	{
        public ListView Menu { get; set; }

        public MenuPage()
        {
            Icon = "settings.png";
            Title = "menu"; // The Title property must be set.
            BackgroundColor = Color.FromHex("333333");

            Menu = new MenuListView();

            var menuLabel = new ContentView
            {
                Padding = new Thickness(10, 36, 0, 5),
                Content = new Label
                {
                    TextColor = Color.FromHex("AAAAAA"),
                    Text = "MENU",
                }
            };

            var layout = new StackLayout
            {
                Spacing = 0,
                VerticalOptions = LayoutOptions.FillAndExpand
            };
            layout.Children.Add(menuLabel);
            layout.Children.Add(Menu);

            Content = layout;
        }
	}

 

    public class MenuItem
    {
	    public string Title { get; set; }
 
	    public bool Setting { get; set; }

        public string Key { get; set; }
    }

    public class MenuListView : ListView
    {
        public MenuListView()
        {
            var data = new List<SwitchCell>();
            bool allAbilities = false;
            if (Application.Current.Properties.ContainsKey("ShowAllAbilities") && Application.Current.Properties["ShowAllAbilities"] is bool)
                allAbilities = (bool)Application.Current.Properties["ShowAllAbilities"];

            var showAllAbilities = new SwitchCell()
            {
                Text = "Se alla färdigheter",
                On = allAbilities,
            };
            showAllAbilities.OnChanged += (sender, args) => UpdateSetting(sender, args, "ShowAllAbilities");
            data.Add(showAllAbilities);

            var clearData = new SwitchCell()
            {
                Text = "Rensa all KaraktärsData",
                On = false
            };
            clearData.OnChanged += (sender, args) => ClearSettings(sender, args);
            data.Add(clearData);
            

            ItemsSource = data;
            VerticalOptions = LayoutOptions.FillAndExpand;
            BackgroundColor = Color.Transparent;

            var cell = new DataTemplate(typeof(SwitchCell));
            cell.SetBinding(SwitchCell.TextProperty, "Text");
            cell.SetBinding(SwitchCell.OnProperty, "On");


            ItemTemplate = cell;
        }

        private void UpdateSetting(object sender, ToggledEventArgs args, string showabilities)
        {
            Application.Current.Properties[showabilities] = args.Value;
        }

        private void ClearSettings(object sender, ToggledEventArgs args)
        {
            Application.Current.Properties["ListOfCharacter"] = null;
            Application.Current.Properties["Character"] = null;
            Application.Current.Properties["Name"] = null;
            Application.Current.Properties["Kroklista"] = null;
        }
    }

    //public class MenuListData : List<SwitchCell>
    //{
    //    public MenuListData()
    //    {
    //        bool allAbilities = false;
    //        if (Application.Current.Properties.ContainsKey("ShowAllAbilities") && Application.Current.Properties["ShowAllAbilities"] is bool)
    //            allAbilities = (bool)Application.Current.Properties["ShowAllAbilities"];

    //        var showAllAbilities = new SwitchCell()
    //        {
    //            Text = "Se alla färdigheter",
    //            On = allAbilities,
    //        };
    //        showAllAbilities.OnChanged += (sender, args) => UpdateSetting(sender, args, "ShowAbilities");
    //        this.Add(showAllAbilities);

    //    }

        
    //}
}
